package com.chandra.pos.customer.service.impl;

import com.chandra.pos.customer.dao.entity.*;
import com.chandra.pos.customer.model.*;
import com.chandra.pos.customer.service.CustomerRole;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.persistence.EntityExistsException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 8/15/14.
 */
@Component
public class CustomerRoleImpl implements CustomerRole {

    @Autowired
    CustomerRepository customerRepository;

    @Autowired
    CustomerOrderRepository customerOrderRepository;

    @Autowired
    CustomerPaymentRepository customerPaymentRepository;



    public CustomerModel saveCustomer(CustomerModel customerModel) throws EntityExistsException {
        CustomerEntity customerEntity = new CustomerEntity();

        CustomerEntity customerEntity1 = customerRepository.findByNickNameIgnoreCase(customerModel.getNickName());

        if (customerEntity1 != null && !customerEntity1.getCustomerId().equals(customerModel.getCustomerId()))
            throw new EntityExistsException("dsds");
        BeanUtils.copyProperties(customerModel, customerEntity);
        customerEntity = customerRepository.save(customerEntity);

        customerModel.setCustomerId(customerEntity.getCustomerId());
        return customerModel;
    }

    @Override
    public CustomerList search(CustomerFilter customerFilter) {

        CustomerList customerList = new CustomerList();

        List<CustomerModel> customerModels = new ArrayList<>();
        Page<CustomerEntity> page = null;

        PageRequest request =
                new PageRequest(customerFilter.getPage() - 1, customerFilter.getRows(), customerFilter.getDBSord(), customerFilter.getSidx());


        if (!StringUtils.isEmpty(customerFilter.getQuery()) && customerFilter.getCustomerType() != null)

            page = customerRepository.search(customerFilter.getQuery(), customerFilter.getCustomerType(), request);
        else if (!StringUtils.isEmpty(customerFilter.getQuery()))

            page = customerRepository.search(customerFilter.getQuery() + "%", request);

        else if (customerFilter.getCustomerType() !=null)

            page = customerRepository.search(customerFilter.getCustomerType(), request);
        else
            page = customerRepository.findAll(request);


        for (CustomerEntity customerEntity : page.getContent()) {
            CustomerModel customerModel = new CustomerModel();

            BeanUtils.copyProperties(customerEntity, customerModel);

            customerModels.add(customerModel);
        }
        customerList.setPage(customerFilter.getPage());
        customerList.setRecords(page.getTotalElements());
        customerList.setTotal(page.getTotalPages());

        customerList.setCustomers(customerModels);
        return customerList;
    }





    @Override
    public CustomerModel getCustomerDetails(CustomerFilter customerFilter) {
        CustomerEntity orderEntity = customerRepository.findOne(customerFilter.getCustomerId());

        CustomerModel orderModel1 = new CustomerModel();

        BeanUtils.copyProperties(orderEntity, orderModel1);
        return orderModel1;
    }
}
